<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><s:if test="dictionaryType.dicTypeId==null"><s:text name="dictionaryType.add.title"/></s:if><s:else><s:text name="dictionaryType.edit.title"/></s:else></title>
	<%@ include file="/common/meta.jsp"%>
	<%@ include file="/common/common.jsp"%>
</head>
<body>
<div id="wrapper">
	<div class="curmb">您现在的位置:<a href=" main.action">首页</a><span>></span>数据字典管理</div>

	<div class="listtit" id="listtit"><p><span><strong>数据字典列表</strong></span></p></div>
	<s:form action="dictionarytype-save" id="action_form">
	<s:hidden name="dictionaryType.dicTypeId" id="dictionarytype.dicTypeId"/>
	<s:hidden name="dictionaryType.dicCode" id="dictionarytype.dicCode"/>
	<s:hidden name="dictionaryType.dicDelCode" id="dictionarytype.dicDelCode"/>
	<div class="searchbox">
		<table class="searchTable">
			<tbody id="searchTable">
				<tr>
					<td class="text"><s:text name="dictionaryType.dicTypeCode"/>:</label></td>
					<td class="input"><s:textarea type="text" name="dictionaryType.dicTypeCode" id="qry_dictionaryKey" cssClass="txtin300"/></td>
					<td class="text"><s:text name="dictionaryType.isModify"/>: </label></td>
					<td class="input"><s:if test="dicIsModify!=null"><s:select name="dictionaryType.isModify" list="dicIsModify" listKey="dictionaryKey" listValue="dictionaryValue" value="dictionaryType.isModify" cssClass="selectClass"/></s:if></td>
					
				</tr>
				<tr>
					<td class="text"><s:text name="dictionaryType.dicTypeName"/>: </label></td>
					<td class="input"><s:textarea type="text" name="dictionaryType.dicTypeName" id="qry_dictionaryKey" cssClass="txtin300"/></td>
					<td class="text"><s:text name="dictionaryType.isEnable"/>:</label></td>
					<td class="input"><s:if test="dicIsEnable!=null"><s:select name="dictionaryType.isEnable" list="dicIsEnable" listKey="dictionaryKey" listValue="dictionaryValue" value="dictionaryType.isEnable" cssClass="selectClass"/></s:if></td>
					
				</tr>
				<tr>
					<td class="text" ><s:text name="dictionaryType.memo"/>:</td>
					<td class="input" colspan="3"><s:textarea name="dictionaryType.memo" cssClass="txtin300"/></td>
				</tr>
			</tbody>
		</table>
		<div class="clear"></div>
	</div>

	<div class="listoperate">
		<span class="cssa"><input onclick="addf()" id="top_new_btn" type="button" value="添加子类" class="btnadd"/></span>
		<span>
			<b>字典类型下属子类</b>
		</span>
	</div>
	<div class="listbox" id="listbox">

		<table class="listgrid" id="listbox">
			<tbody>
				<tr id="trtitle">
					<th width="5%" style='display:none'><input type="checkbox" name="all" id="all_check"/></th>
					<th width="27%">字典KEY值</th>
					<th width="20%">字典VALUE值</th>
					<th width="25%">备注</th>

					<th width="8%">排序</th>

					<th width="15%">操作</th>
				</tr>
				<s:iterator value="dictionarys" status="index">
					<tr id="l1_<s:property value="#index.index+1"/>">
						<td style='display:none'><input type="checkbox" name="key" value="<s:property value="dictionaryId"/>"/></td>
						<td align="left"><img <s:if test="(\"1\").equals(isLeaf)">src="${ctx}/imgs/minus_f.png"</s:if><s:else>src="${ctx}/imgs/plus_e.png" </s:else> id="il1_<s:property value="#index.index+1"/>"/>&nbsp;<s:textfield name="dictionaryKey" cssClass="txtin150"></s:textfield></td>
						<td><s:textfield name="dictionaryValue" cssClass="txtin150"></s:textfield></td>
						<td><s:textfield name="memo" cssClass="txtin300"></s:textfield></td>
						<td><s:textfield name="ord" size="3"></s:textfield></td>
						<td>
							<a href="#this" onclick="add(this)">添加</a><tt>|</tt>
							<a href="#this" onclick="del(this);">删除</a>
						</td>
					</tr>
				</s:iterator>
			</tbody>
		</table>
	</div>
	<div class="listoperateb ">
	</div>
	<div class="searchbox">
		<div style="padding-left: 40%">
			<span>
				<input type="button" onclick="save_form();" value="保存" class="btnlist2" id="bottom_anti_btn"/>
	
				<input type="button" value="重置" class="btnlist2" id="bottom_del_btn"/>
				
				<input type="button" value="返回" class="btnlist2" id="bottom_del_btn" onclick="javascript:history.go(-1);"/>
			</span>
		</div>
	</div>
	</s:form>
</div>
</body>
</html>
<script type="text/javascript" defer="defer">
	actionName = "dictionary";
	var hs=0;//tr行数
	var flag=true;//通过状态
	function add(obj){
		var ftr = $(obj).parent().parent();
		var f=ftr.attr("id").split("_");
		//alert(f.length)
		var zc = f.length;//层数
		if(zc>5){
			alert("只能添加四级结构");
			return;
		}else{
			$("#i"+ftr.attr("id")).attr("src","${ctx}/imgs/minus_e.png");
			$("#i"+ftr.attr("id")).unbind('click').click(function(){
				closetr(this);
			});
		}
		var ztr = $("tr[id^='"+ftr.attr("id")+"_']");
		var maxxl=0;//改组最大的序列
		for(var i=0;i<ztr.length;i++){
			var xl = ztr[i].id.split("_")[f.length];//当前序列
			maxxl = maxxl<parseInt(xl)?parseInt(xl):maxxl;
		}
		var zid=ftr.attr("id")+"_"+(parseInt(maxxl)+1);		
		var html="<tr id='"+zid+"'><td style='display:none'><input type='checkbox' value='"+zid+"'/></td>";
		html+="<td align='left'><img src='${ctx}/imgs/minus_f.png' id='i"+zid+"' style='margin-left:"+parseInt(zc)*15+"px'/>&nbsp;<input type='text' class='txtin150'/></td>";
		html+="<td><input type='text'  class='txtin150'/></td>";
		html+="<td><input type='text'  class='txtin300'/></td>";
		html+="<td><input type='text'  size='3'/></td>";			
		html+="<td><a href='#this' onclick='add(this)'>添加</a><tt>|</tt><a href='#this' onclick='del(this);'>删除</a></td></tr>"	;
		if(ztr.length==0){
			ftr.after(html);
		}
		else{
			$(ztr[ztr.length-1]).after(html);
		}
	}
	//增加一级栏目
	function addf(){
		var ftr = $("tr[id^='l1_']");
		//alert(ftr.length)
		if(ftr.length==0){
			var html="<tr id='l1_1'><td style='display:none'><input type='checkbox' name='id' value='l1_1'/></td>";
			html+="<td align='left'><img src='${ctx}/imgs/minus_f.png' id='il1_1'/>&nbsp;<input type='text' class='txtin150'/></td>";
			html+="<td><input type='text'  class='txtin150'/></td>";
			html+="<td><input type='text'  class='txtin300'/></td>";
			html+="<td><input type='text'  size='3'/></td>";			
			html+="<td><a href='#this' onclick='add(this)'>添加</a><tt>|</tt><a href='#this' onclick='del(this);'>删除</a></td></tr>"	;
			$("#trtitle").after(html); 
		}	
		else{
			var maxc = 0;//最大一栏目数
			for(var i=0;i<ftr.length;i++){
				maxc = (maxc<parseInt(ftr[i].id.split("_")[1]))?ftr[i].id.split("_")[1]:maxc;
			}
			var fid = "l1_"+(parseInt(maxc)+1);
			var html="<tr id='"+fid+"'><td style='display:none'><input type='checkbox' name='id' value='"+fid+"'/></td>";
			html+="<td align='left'><img  src='${ctx}/imgs/minus_f.png' id='i"+fid+"'/>&nbsp;<input type='text' class='txtin150'/></td>";
			html+="<td><input type='text'  class='txtin150'/></td>";
			html+="<td><input type='text'  class='txtin300'/></td>";
			html+="<td><input type='text'  size='3'/></td>";			
			html+="<td><a href='#this' onclick='add(this)'>添加</a><tt>|</tt><a href='#this' onclick='del(this);'>删除</a></td></tr>"	;
			$(ftr[ftr.length-1]).after(html); 
		}		
	}
	
	function del(obj){
		if(!confirm("确定要删除栏目")){			
			return ;
		}
		var ftr = $(obj).parent().parent();
		del_id(ftr);
		var ztr = $("tr[id^='"+ftr.attr("id")+"_']");
		for(var i=0;i<ztr.length;i++){	
			del_id($(ztr[i]));
			($("#"+ztr[i].id)).remove();//子元素移除
			}
		ftr.remove();//移除自己
		var fc = ftr.attr("id").split("_").length-1;
		if(fc>0){
			var f_ftr = $("#"+ftr.attr("id").substring(0,ftr.attr("id").lastIndexOf("_")));//删除栏目的父栏目
			if($("tr[id^='"+f_ftr.attr("id")+"_']").length>0){//有子栏目
				return ;
			}
			else{
				$("#i"+f_ftr.attr("id")).attr("src","${ctx}/imgs/minus_f.png");
			}			
		}
	}
	
	function opentr(obj){
		var fid = $("#"+obj.id.substring(1)+" > td > input")[0].value;
		if(fid.length<32||$("tr[id^='"+obj.id.substring(1)+"_']").length>0){
			var ztr = $("tr[id^='"+$(obj).attr("id").substring(1)+"_']");
			if(ztr.length==0){
				return;
			}
			for(var i=0;i<ztr.length;i++){
				$(ztr[i]).show();//.style.display="block";
			}
		}else{
			$.ajax({	
				url:"${ctx}/dictionary/dictionarytype-child.action?time="+new Date().getTime(),
				data:{"dictionaryType.dicTypeId":fid},
				dataType:"text",
				type:"post",
				success:function(data){
					var json = eval(data);
					for(var i=0;i<json.length;i++){
						var html="<tr id='"+obj.id.substring(1)+"_"+(i+1)+"'><td style='display:none'><input type='checkbox' value='"+json[i].dictionaryId+"'/></td>";
						html+="<td align='left'>";
						if(json[i].isLeaf=="1"){
							html+="<img src='${ctx}/imgs/minus_f.png' id='i"+obj.id.substring(1)+"_"+(i+1)+"' style='margin-left:"+parseInt(obj.id.split("_").length)*15+"px'/>&nbsp;";
						}else{
							html+="<img src='${ctx}/imgs/plus_e.png' id='i"+obj.id.substring(1)+"_"+(i+1)+"' style='margin-left:"+parseInt(obj.id.split("_").length)*15+"px'/>&nbsp;";
						}
						html+="<input type='text' class='txtin150' value='"+json[i].dictionaryKey+"'/></td>";
						html+="<td><input type='text'  class='txtin150' value='"+json[i].dictionaryValue+"'/></td>";
						if(json[i].memo=='null'){
							html+="<td><input type='text'  class='txtin300' value=''/></td>";
						}else{
							html+="<td><input type='text'  class='txtin300' value='"+json[i].memo+"'/></td>";
						}
						html+="<td><input type='text'  size='3' value='"+json[i].ord+"'/></td>";			
						html+="<td><a href='#this' onclick='add(this)'>添加</a><tt>|</tt><a href='#this' onclick='del(this);'>删除</a></td></tr>";
						var ftr = $(obj).parent().parent();
						ftr.after(html);
						if(json[i].isLeaf=="0"){
							$($("#i"+obj.id.substring(1)+"_"+(i+1))).unbind('click').click(function(){
								opentr(this);
							});
						}
					}
				}
			});
		}
		$(obj).attr("src","${ctx}/imgs/minus_e.png");
		$(obj).unbind('click').click(function(){
			closetr(this);
		});
	}
	
	function closetr(obj){
		var ztr = $("tr[id^='"+$(obj).attr("id").substring(1)+"_']");
		if(ztr.length==0){
			return;
		}
		for(var i=0;i<ztr.length;i++){
			$(ztr[i]).hide();
		}
		$(obj).attr("src","${ctx}/imgs/plus_e.png");
		$(obj).unbind('click').click(function(){
			opentr(this);
		});
	}
	//删除ID列表
	function del_id(obj){
		var id=$("#"+obj.attr("id")+" > td > input")[0].value;//一行数据ID值
		var del_id =$("input[name=dictionaryType.dicDelCode]").val();
		if(id!=""){
			if(del_id!=""){
				$("input[name=dictionaryType.dicDelCode]").val(del_id+","+id);
			}
			else{
				$("input[name=dictionaryType.dicDelCode]").val(id);
			}
		}
		
		
	}
	//得到栏目下所有信息 jquery对象obj
	function getInfo(obj){
		var infoid =$("#"+obj.attr("id")+" > td > input")[0].value;
		if(infoid==""){
			//为空则为栏目ID,其子栏目不可能为数据库中读取的记录，故下述父栏目肯定为该栏目ID
			infoid=obj.attr("id");
			$("#"+obj.attr("id")+" > td > input")[0].value=obj.attr("id");
		}
		
		if(obj.attr("id").split("_").length==2){//为顶级栏目时，fid为空
			fid="0";
		}
		else{
			var fid=$("#"+obj.attr("id").substring(0,obj.attr("id").lastIndexOf("_"))+" > td > input")[0].value;
		}
		var key=$("#"+obj.attr("id")+" > td > input")[1].value;
		var va = $("#"+obj.attr("id")+" > td > input")[2].value;
		var px = $("#"+obj.attr("id")+" > td > input")[4].value;
		var bz=$("#"+obj.attr("id")+" > td > input")[3].value;
		if(key==""&&flag){
			//alert("请输入"+hs+"行的KEY值");
			flag=false;
		}
		if(va==""&&flag){
			//alert("请输入"+hs+"行的VALUE值");
			flag=false;
		}
		if(px==""){
			//alert("请输入"+hs+"行的排序值");
			px="0";
			//flag=false;
		}	
		if(bz==""){
			bz=" ";
		}
		var dsj = infoid+"|"+key+"|"+va+"|"+bz+"|"+px+"|"+fid;
		//alert(dsj)
		var sj = $("input[name=dictionaryType.dicCode]").val();
		if(sj==""){
			$("input[name=dictionaryType.dicCode]").val(dsj);
		}
		else{
			$("input[name=dictionaryType.dicCode]").val(sj+"@"+dsj);
		}
	}
	function save_form(){
		if($("textarea[name=dictionaryType.dicTypeCode]").val()==""){
			alert("字典编码不能为空！");
			return false;
		}
		if($("textarea[name=dictionaryType.dicTypeName]").val()==""){
			alert("字典名称不能为空！");
			return false;
		}
		var a = true;
		$("tr[id^='l1_']").each(function(){
			if($("#"+$(this).attr("id")+" > td > input")[1].value==""&&a){
				alert("字典KEY值不能为空！");
				a = false;
			}
			if($("#"+$(this).attr("id")+" > td > input")[2].value==""&&a){
				alert("字典VALUE值不能为空！");
				a = false;
			}
		});
		if(!a){
			return false;
		}
		flag=true;
		hs=0;
		$("input[name=dictionaryType.dicCode]").val("");
		var ftr = $("tr[id^='l1_']");
		for(var i=0;i<ftr.length;i++){
			hs++;
			getSonLM(ftr[i]);
		}
		//alert($("input[name=dictionaryType.dicCode]").val());
		//alert("删除了的元素\n"+$("input[name=dictionaryType.dicDelCode]").val());
		//alert(flag);
		//$("#action_form").submit();
	}
	//js tr对象  
	function getSonLM(obj){
		getInfo($(obj));
		var ztr = $("tr[id^='"+$(obj).attr("id").substring(1)+"_']");
		for(var i=0;i<ztr.length;i++){
			hs++;
			getInfo($(ztr[i]));
			getSonLM(ztr[i]);
		}
	}
	$(document).ready(function(){
		<s:iterator value="dictionarys" status="index">
		<s:if test="(\"0\").equals(isLeaf)">
			$($("#il1_<s:property value="#index.index+1"/>")).unbind('click').click(function(){
				opentr(this);
			});
		</s:if>
		</s:iterator>
	});
</script>